package com.utils.basehelper.string;

import java.util.Calendar;

/**
 *****************************************************************
 *               Program NAME: 字符串处理工具类
 *                 programmer: Ouyangsheng (Modify)
 *                Create DATE: 2002.04.17
 *             Create address: Beijing
 *                Modify DATE:
 *             Modify address:
 *****************************************************************
 *
 *                  对字符串操作的工具类。
 *
 *****************************************************************
 */
public class StrDateTool {
  public static final String EQUAL = "=";
  public static final String GREATER = ">";
  public static final String GREATGE_EQUAL = ">=";
  public static final String LESS = "<";
  public static final String LESS_EQUAL = "<=";
  public static final String NOT_EQUAL = "!=";
  public static final String CONTAIN = "*";
  public static final String BETWEEN = ":";
  public static final String DATEDELIMITER = "/";
  public static final String VISADATEDELIMITER = "-";
  public static final String TIMEDELIMITER = ":";
  public static final String ADDRESSDELIMITER = "$$";
  public static final String DELIMITER = "^";
  public static final String PACKAGESPILTER = "|";
  public static final String OR = "~!";
  public static final int LENGTH_OR = 2;
  public static final String BETWEEN_AND = ":";
  public static final String BLANK = "?";
  public static final String DIRECTMODE = "Direct Mode!" + DELIMITER +
      DELIMITER;

  /**
   * 将字符串按照指定的分隔字符进行拆分,返回从指定序号的分隔符到前一个分隔符之间的字符串
   * @param strMain String 主字符串
   * @param strDelimiters String 分隔符
   * @param intSerialNo int 分隔符序号
   * @return String 指定序号的分隔符到前一个分隔符之间的字符串,如果没有找到则返回""
   * 例如：值赋串类似于 值1|值2|值3|值4|
   * 则intSerialNo=0 return ""   intSerialNo=1 return "值1"  intSerialNo=5 return ""
   */
  public static String decodeStr(String strMain, String strDelimiters,
			int intSerialNo) {
		int intIndex = 0; /* 分隔符出现在主字符串中的起始位置 */
		int intCount = 0; /* 在扫描主字符串的过程中,第几次遇到分隔符字符串 */
		String strReturn = ""; /* 作为返回值的字符串 */

		if (strMain.length() < strDelimiters.length()) {
			return ""; /* 若主字符串比分隔符串还要短的话,则返回空字符串 */
		}

		intIndex = strMain.indexOf(strDelimiters);
		if (intIndex == -1) {
			return ""; /* 若主字符串中不存在分隔符,则返回空字符串 */
		}

		while (intIndex != -1) /* 未找到分隔符时退出循环,并返回空字符串 */
		{
			strReturn = strMain.substring(0, intIndex);
			intCount++;
			if (intCount == intSerialNo) {
				if (intIndex == 0) {
					return "";
				} else {
					return strReturn.trim();
				}
			}
			strMain = strMain.substring(intIndex + 1);
			intIndex = strMain.indexOf(strDelimiters);
		}
		return "";
	}

  /**
   * 获取子串在主串中出现第 n 次的位置
   * @param strMain String 主字符串
   * @param strSub String 子字符串
   * @param intTimes int 出现次数
   * @return int 位置值,如果子串在主串中没有出现指定次数,则返回-1
   */
	public static int getPos(String strMain, String strSub, int intTimes) {
		int intCounter = 0; // 循环记数
		int intPosition = 0; // 位置记录
		int intLength = strSub.length(); // 子串长度

		if (intTimes <= 0) {
			return -1;
		}
		while (intCounter < intTimes) {
			intPosition = strMain.indexOf(strSub, intPosition);
			if (intPosition == -1) {
				return -1;
			}
			intCounter++;
			intPosition += intLength;
		}
		return intPosition - intLength;
	}

  /**
   * 获取从指定位置开始子串在主串中出现第 n 次的位置
   * @param strMain String 主字符串
   * @param strSub String 子字符串
   * @param intStartIndex int 起始位置
   * @param intTimes int 出现次数
   * @return int 位置值,如果从起始位置起子串在主串中没有出现指定次数,则返回-1
   */
  public static int getPos(String strMain, String strSub, int intStartIndex,
			int intTimes) {
		if (strMain.length() - 1 < intStartIndex) {
			return -1;
		}
		int intPosition = getPos(strMain.substring(intStartIndex), strSub, intTimes);
		if (intPosition != -1) {
			intPosition += intStartIndex;
		}
		return intPosition;
	}


  /**
   * 由给定日期字符串获取格式日期(年/月/日)
   * @param strYear String 年
   * @param strMonth String 月
   * @param strDay String 日
   * @return String 格式日期：年/月/日
   */
	public static String getDate(String strYear, String strMonth, String strDay) {
		String strReturn = "";
		StringBuffer tSBql = new StringBuffer();
		int intYear = 0;
		int intMonth = 0;
		int intDay = 0;
		if ((strYear != null) && (strMonth != null) && (strDay != null) && (strYear.trim().length() > 0)
				&& (strMonth.trim().length() > 0) && (strDay.trim().length() > 0)) {
			try {
				intYear = new Integer(strYear).intValue();
				intMonth = new Integer(strMonth).intValue();
				intDay = new Integer(strDay).intValue();
			} catch (Exception exception) {
				return strReturn;
			}

			if ((intYear <= 0) || (intMonth <= 0) || (intMonth > 12) || (intDay <= 0) || (intDay > 31)) {
				strReturn = "";
			} else {
				tSBql.append(intYear);
				if (intMonth < 10) {
					tSBql.append(StrDateTool.DATEDELIMITER);
					tSBql.append("0");
					tSBql.append(intMonth);
				} else {
					tSBql.append(StrDateTool.DATEDELIMITER);
					tSBql.append(intMonth);
				}

				if (intDay < 10) {
					tSBql.append(StrDateTool.DATEDELIMITER);
					tSBql.append("0");
					tSBql.append(intDay);
				} else {
					tSBql.append(StrDateTool.DATEDELIMITER);
					tSBql.append(intDay);
				}
				strReturn = tSBql.toString();
			}
		}
		return strReturn;
	}

  /**
   * 获得系统日期(年/月/日)
   * @return String 格式日期：年/月/日
   */
	public static String getDate() {
		StringBuffer tSBql = new StringBuffer();
		int intYear = Calendar.getInstance().get(Calendar.YEAR);
		int intMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int intDate = Calendar.getInstance().get(Calendar.DATE);

		tSBql.append(intYear);

		if (intMonth < 10) {
			tSBql.append(StrDateTool.DATEDELIMITER);
			tSBql.append("0");
			tSBql.append(intMonth);
		} else {
			tSBql.append(StrDateTool.DATEDELIMITER);
			tSBql.append(intMonth);
		}

		if (intDate < 10) {
			tSBql.append(StrDateTool.DATEDELIMITER);
			tSBql.append("0");
			tSBql.append(intDate);
		} else {
			tSBql.append(StrDateTool.DATEDELIMITER);
			tSBql.append(intDate);
		}
		return tSBql.toString();
	}

  /**
   * 由给定日期字符串获取格式日期(年/月)
   * @param strYear String 年
   * @param strMonth String 月
   * @return String 格式日期：年/月
   */
	public static String getDate(String strYear, String strMonth) {
		String strReturn = "";
		StringBuffer tSBql = new StringBuffer();
		int intYear = 0;
		int intMonth = 0;
		if ((strYear != null) && (strMonth != null) && (strYear.trim().length() > 0)
				&& (strMonth.trim().length() > 0)) {
			intYear = new Integer(strYear).intValue();
			intMonth = new Integer(strMonth).intValue();
			if ((intYear <= 0) || (intMonth <= 0) || (intMonth > 12)) {
				strReturn = "";
			} else {
				tSBql.append(intYear);
				tSBql.append(StrDateTool.DATEDELIMITER);
				tSBql.append(intMonth);
				strReturn = tSBql.toString();
			}
		}
		return strReturn;
	}

  /**
   * 获取日期值中的年份
   * @param strDate String 日期（年/月/日）
   * @return String 年
   */
	public static String getYear(String strDate) {
		int intPosition = 0;
		String strReturn = "";
		int intYear = 0;

		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition = StrDateTool.getPos(strDate, StrDateTool.DATEDELIMITER, 1);
			if (intPosition > 0) {
				strReturn = strDate.substring(0, intPosition);
				intYear = new Integer(strReturn).intValue();
				if ((intYear <= 0)) {
					strReturn = "";
				} else {
					strReturn = "" + intYear;
				}

				if ((intYear < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

  /**
   * 获取系统日期中的年份
   * @return String 年
   */
  public static String getYear() {
    StringBuffer tSBql = new StringBuffer();
    int intYear = Calendar.getInstance().get(Calendar.YEAR);
    tSBql.append(intYear);
    return tSBql.toString();
  }

  /**
   * 获取日期值中的月份
   * @param strDate String 日期
   * @return String 月
   */
	public static String getMonth(String strDate) {
		int intPosition1 = 0, intPosition2 = 0;
		String strReturn = "";
		int intMonth = 0;
		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition1 = StrDateTool.getPos(strDate, StrDateTool.DATEDELIMITER, 1);
			intPosition2 = StrDateTool.getPos(strDate, StrDateTool.DATEDELIMITER, 2);
			if ((intPosition1 > 0) && intPosition2 > intPosition1) {

				strReturn = strDate.substring(intPosition1 + 1, intPosition2);

				intMonth = new Integer(strReturn).intValue();
				if ((intMonth <= 0) || (intMonth > 12)) {
					strReturn = "";
				} else {
					strReturn = "" + intMonth;
				}

				if ((intMonth < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

  /**
   * 获取系统日期中的月份
   * @return String 月
   */
	public static String getMonth() {
		StringBuffer tSBql = new StringBuffer();
		int intMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		if (intMonth < 10) {
			tSBql.append("0");
			tSBql.append(intMonth);
		} else {
			tSBql.append(intMonth);
		}
		return tSBql.toString();

	}

  /**
   * 获取给定日期值中的天
   * @param strDate String 日期
   * @return String 天
   */
	public static String getDay(String strDate) {
		int intPosition = 0;
		String strReturn = "";
		int intDay = 0;
		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition = StrDateTool.getPos(strDate, StrDateTool.DATEDELIMITER, 2);
			if (intPosition > 0) {

				strReturn = strDate.substring(intPosition + 1);

				intDay = new Integer(strReturn).intValue();

				if ((intDay <= 0) || (intDay > 31)) {
					strReturn = "";
				} else {
					strReturn = "" + intDay;
				}

				if ((intDay < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

  /**
   * 获取系统日期值中的天
   * @return String 天
   */
	public static String getDay() {
		StringBuffer tSBql = new StringBuffer();
		int intDate = Calendar.getInstance().get(Calendar.DATE);
		if (intDate < 10) {
			tSBql.append("0");
			tSBql.append(intDate);
		} else {
			tSBql.append(intDate);
		}
		return tSBql.toString();
	}

  /**
   * 获取系统时间值
   * @return String 小时: 分钟：秒
   */
	public static String getTime() {
		StringBuffer tSBql = new StringBuffer();
		tSBql.append(StrDateTool.getHour());
		tSBql.append(":");
		tSBql.append(StrDateTool.getMinute());
		tSBql.append(":");
		tSBql.append(StrDateTool.getSecond());

		return tSBql.toString();
	}

  /**
   * 获取系统时间值中的小时
   * @return String 小时
   */
	public static String getHour() {
		StringBuffer tSBql = new StringBuffer();
		int intHour = Calendar.getInstance().get(Calendar.HOUR)
				+ (Calendar.HOUR_OF_DAY + 1) * Calendar.getInstance().get(Calendar.AM_PM);
		if (intHour < 10) {
			tSBql.append("0");
			tSBql.append(intHour);
		} else {
			tSBql.append(intHour);
		}

		return tSBql.toString();
	}

  /**
   * 获取系统时间值中的分钟
   * @return String 分钟
   */
	public static String getMinute() {
		StringBuffer tSBql = new StringBuffer();
		int intMinute = Calendar.getInstance().get(Calendar.MINUTE);
		if (intMinute < 10) {
			tSBql.append("0");
			tSBql.append(intMinute);
		} else {
			tSBql.append(intMinute);
		}

		return tSBql.toString();
	}

  /**
   * 获取系统时间值中秒数
   * @return String 秒数
   */
	public static String getSecond() {
		StringBuffer tSBql = new StringBuffer();
		int intSecond = Calendar.getInstance().get(Calendar.SECOND);
		if (intSecond < 10) {
			tSBql.append("0");
			tSBql.append(intSecond);
		} else {
			tSBql.append(intSecond);
		}

		return tSBql.toString();
	}

  /**
   * 获取以"-"分隔日期值中的年份
   * @param strDate String 日期
   * @return String 年
   */
	public static String getVisaYear(String strDate) {
		int intPosition = 0;
		String strReturn = "";
		int intYear = 0;

		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition = StrDateTool.getPos(strDate, StrDateTool.VISADATEDELIMITER, 1);
			if (intPosition > 0) {
				strReturn = strDate.substring(0, intPosition);
				intYear = new Integer(strReturn).intValue();
				if ((intYear <= 0)) {
					strReturn = "";
				} else {
					strReturn = "" + intYear;
				}

				if ((intYear < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

  /**
   * 获取以"-"分隔日期值的中文表示
   * @param strDate String 日期
   * @return String YYYY年MM月DD日
   */
	public static String getChnDate(String strDate) {
		String strReturn = getVisaYear(strDate) + "年" + getVisaMonth(strDate) + "月" + getVisaDay(strDate) + "日";
		return strReturn;
	}

  /**
   * 获取以"-"分隔日期值中的月份
   * @param strDate String 日期
   * @return String 月
   */
	public static String getVisaMonth(String strDate) {
		int intPosition1 = 0, intPosition2 = 0;
		String strReturn = "";
		int intMonth = 0;
		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition1 = StrDateTool.getPos(strDate, StrDateTool.VISADATEDELIMITER, 1);
			intPosition2 = StrDateTool.getPos(strDate, StrDateTool.VISADATEDELIMITER, 2);
			if ((intPosition1 > 0) && intPosition2 > intPosition1) {

				strReturn = strDate.substring(intPosition1 + 1, intPosition2);

				intMonth = new Integer(strReturn).intValue();
				if ((intMonth <= 0) || (intMonth > 12)) {
					strReturn = "";
				} else {
					strReturn = "" + intMonth;
				}

				if ((intMonth < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

  /**
   * 获取以"-"分隔日期值中的天
   * @param strDate String 日期
   * @return String 天
   */
	public static String getVisaDay(String strDate) {
		int intPosition = 0;
		String strReturn = "";
		int intDay = 0;
		if ((strDate != null) && (strDate.trim().length() > 0)) {
			intPosition = StrDateTool.getPos(strDate, StrDateTool.VISADATEDELIMITER, 2);
			if (intPosition > 0) {

				strReturn = strDate.substring(intPosition + 1);

				intDay = new Integer(strReturn).intValue();

				if ((intDay <= 0) || (intDay > 31)) {
					strReturn = "";
				} else {
					strReturn = "" + intDay;
				}

				if ((intDay < 10) && (!strReturn.equals(""))) {
					strReturn = "0" + strReturn;
				}
			}
		}
		return strReturn;
	}

}
